//
//  JKCommentCell.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/12.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

let deleteAttr = NSAttributedString(string: "  抱歉，原点评已经被删除")

protocol JKCommentCellProtocol: NSObjectProtocol {
    
    /** 展开和收起引用评论 */
    func commentCellDidClickUnfoldButton(cell: JKCommentCell, button: UIButton)
}

class JKCommentCell: JKBaseTableViewCell {
    
    // MARK: - 公共属性
    @IBOutlet weak var separatorLineView: UIView!
    
    weak var delegate: JKCommentCellProtocol?
    
    // MARK: - 私有属性
    
    /** 头像 */
    @IBOutlet private var iconImageView: UIImageView!
    
    /** 夜间模式遮盖头像的imagView */
    @IBOutlet private var iconMaskImgView: UIImageView!
    
    /** 用户名 */
    @IBOutlet private var nameLabel: UILabel!
    
    /** 评论内容 */
    @IBOutlet private var contentLabel: UILabel!
    
    /** 引用的评论 */
    @IBOutlet private var refContentLabel: UILabel!
    
    /** 时间 */
    @IBOutlet private var timeLabel: UILabel!
    
    /** 点赞数量label */
    @IBOutlet private var zanLabel: UILabel!
    
    /** 点赞后的动画label */
    @IBOutlet private var zanAnimationLabel: UILabel!
    
    /** 点赞图标btn */
    @IBOutlet private var zanIconBtn: UIButton!
    
    /** unfoldButton */
    @IBOutlet private var unfoldButton: UIButton!
    
    @IBOutlet weak var iconCoverView: UIView!
    // MARK: - 初始化
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        iconCoverView.layer.cornerRadius = 15
    }
    
    internal override func initialization() {
        super.initialization()
    }
    
    public var cmtModel: JKCommentModel? {
        
        didSet{
            
            if cmtModel == nil {
                return
            }
            
            let _ = cmtModel!.cellHeight
            
            iconImageView.jk_setCircleIcon(withUrlStr: cmtModel!.avatar)
            
            //            [self.iconImageView setHeaderIcon:cmtModel.avatar]
            self.nameLabel.text = cmtModel!.author
            
            self.contentLabel.attributedText = cmtModel!.contentAttr
            
            self.timeLabel.text = cmtModel!.time == nil ? "" : JKDateTool.timeStrWithTimeStamp(timestamp: cmtModel!.time!, format: "MM-dd HH:mm")
            self.zanLabel.text = cmtModel!.likes
            self.zanLabel.textColor = cmtModel!.isLiked ? JKMainColor : JKColor(128, 128, 128)
            self.zanIconBtn.isSelected = cmtModel!.isLiked
            self.unfoldButton.isHidden = true
            
            self.refContentLabel.isHidden = (cmtModel!.reply_to == nil)
            
            // 引用评论为空 直接返回
            if (cmtModel!.reply_to == nil) { return }
            
            // 引用评论不为空
            
            if (cmtModel!.reply_to!.status != 0) { // 引用评论已被删除
                
                self.refContentLabel.attributedText = deleteAttr
                self.refContentLabel.backgroundColor = (skinModel == JKDaySkinModelValue) ? JKColor(233, 233, 233) : JKColor(38, 38, 38)
                
                self.refContentLabel.textColor = (skinModel == JKDaySkinModelValue) ? JKSameRGBColor(53) : JKColor(118, 118, 118)
                
                self.refContentLabel.font = UIFont.systemFont(ofSize: 12)
                
            }else { // 引用评论未删除
                
                // 展开收起按钮的显示和隐藏
                self.unfoldButton.isHidden = (cmtModel!.reply_to!.cmtContentRefH > 55) ? false : true
                
                // 用模型控制一切！！
                let lines = (cmtModel!.reply_to!.cmtContentRefH > 55) ? 2 : 3
                self.refContentLabel.numberOfLines = cmtModel!.cmtRefUnfolded ? 0 : lines
                self.unfoldButton.isSelected = cmtModel!.cmtRefUnfolded
                
                self.refContentLabel.backgroundColor = UIColor.clear
                self.refContentLabel.font = UIFont.systemFont(ofSize: 14)
                
                // 4.显示字符串
                self.refContentLabel.attributedText = (skinModel == JKDaySkinModelValue) ? cmtModel!.reply_to!.contentRefAttr : cmtModel!.reply_to!.contentRefNightAttr
            }
        }
    }
    
    @IBAction func unfoldButtonClick(_ button: UIButton) {
        
        button.isSelected = !button.isSelected
        
        if delegate == nil { return }
        
        delegate!.commentCellDidClickUnfoldButton(cell: self, button: button)
    }
    
    @IBAction func zanClick() {
        
        guard let cmtModel = self.cmtModel else { return }
        
        self.zanLabel.text = cmtModel.likes
        self.zanLabel.textColor = cmtModel.isLiked ? JKMainColor : JKColor(128, 128, 128)
        self.zanIconBtn.isSelected = cmtModel.isLiked
        
        if !cmtModel.isLiked { return }
        
        self.zanAnimationLabel.isHidden = false
        self.zanAnimationLabel.transform = CGAffineTransformMakeScale(1.5, 1.5)
        
        UIView.animate(withDuration: 1, animations: {
            
            self.zanAnimationLabel.transform = CGAffineTransformScale(self.zanAnimationLabel.transform, 0.1, 0.1)
            self.zanAnimationLabel.transform = CGAffineTransformTranslate(self.zanAnimationLabel.transform, 0, 110)
            
        }) { (_) in
            
            self.zanAnimationLabel.isHidden = true
        }
    }
    
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        super.setHighlighted(highlighted, animated: animated)
        
        iconCoverView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    override var canBecomeFirstResponder: Bool {
        return true
    }
    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

extension JKCommentCell {
    
    override func updateSkin() {
        super.updateSkin()
        
        if cmtModel != nil && cmtModel!.reply_to != nil {
            
            if (cmtModel!.reply_to!.status != 0) { // 引用评论已被删除
                
                self.refContentLabel.backgroundColor = (skinModel == JKDaySkinModelValue) ? JKColor(233, 233, 233) : JKColor(38, 38, 38)
                
                self.refContentLabel.textColor = (skinModel == JKDaySkinModelValue) ? JKSameRGBColor(53) : JKColor(118, 118, 118)
                
            }else { // 引用评论未删除
                
                
                // 4.显示字符串
                self.refContentLabel.attributedText = (skinModel == JKDaySkinModelValue) ? cmtModel!.reply_to!.contentRefAttr : cmtModel!.reply_to!.contentRefNightAttr
            }
        }
        
        if skinModel == JKNightSkinModelValue { // 夜间模式
            
            self.iconMaskImgView.isHidden = false
            self.nameLabel.textColor = JKColor(186, 186, 186)
            self.contentLabel.textColor = JKColor(153, 153, 153)
            self.timeLabel.textColor = JKColor(108, 108, 108)
            self.unfoldButton.backgroundColor = JKColor(38, 38, 38)
            self.separatorLineView.backgroundColor = JKColor(103, 103, 103)
            self.iconCoverView.isHidden = false
            
        }else{ // 日间模式
            
            self.backgroundColor = JKGlobalBgColor
            self.iconMaskImgView.isHidden = true
            self.nameLabel.textColor = JKColor(53, 53, 53)
            self.contentLabel.textColor = JKColor(53, 53, 53)
            self.timeLabel.textColor = JKColor(118, 118, 118)
            self.unfoldButton.backgroundColor = JKColor(203, 217, 233)
            self.separatorLineView.backgroundColor = JKColor(217, 217, 217)
            self.iconCoverView.isHidden = true
        }
        
        if isSelected || isHighlighted {
            
            self.separatorLineView.backgroundColor = (skinModel == JKNightSkinModelValue) ? JKSameRGBColor(118) : JKSameRGBColor(153)
            
            return
        }
        
        self.separatorLineView.backgroundColor = (skinModel == JKNightSkinModelValue) ? JKSameRGBColor(66) : JKSameRGBColor(217)
    }
}
